package com.customer.crm.announcement.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.crm.announcement.pojo.AnnouncementPojo;
import com.customer.crm.announcement.service.impl.AnnouncementServiceImpl;
import com.customer.crm.department.pojo.DepartmentPojo;
import com.customer.crm.department.service.impl.DepartmentServiceImpl;
import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.service.impl.SystemUserServiceImpl;
import com.customer.crm.util.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;

@Controller
public class AnnouncementController {
    @Resource
    private AnnouncementServiceImpl announcementService;
    @Resource
    private SystemUserServiceImpl userService;
   @Resource
   private DepartmentServiceImpl departmentService;
    //点击公告管理到公告首页页面
    @RequestMapping("/system/announcement/announcementAction_list.action")
    public String toAnnouncementList() {
        return "/views/manage/announcement/list_announcement";
    }

    //加载用户数据

    @ResponseBody
    @RequestMapping("/Announcement_findByPage")
    public ResultData announcementFindByPage(long page, long rows, ModelMap modelMap) {
        Page<AnnouncementPojo> page1 = new Page<>(page, rows);
        Page<AnnouncementPojo> page2 = announcementService.page(page1, null);
        modelMap.addAttribute("totalRows", page2.getTotal());
        modelMap.addAttribute("result", page2.getRecords());
        ResultData resultData = ResultData.buildFailureResult("data", modelMap);
        return resultData;
    }

    //添加按钮事件处理函数
    @RequestMapping("/Announcement_add")
    public String addAnnouncement(ModelMap modelMap) {
        List<SystemUser> list = userService.list();
        modelMap.addAttribute("userList",list);
        List<DepartmentPojo> list1 = departmentService.list();
        modelMap.addAttribute("departmentPojo",list1);
        return "/views/manage/announcement/insert_announcement";
    }

    //添加按钮事件处理函数
    @ResponseBody
    @RequestMapping("/Announcement_saveOrUpdate")
    public ModelMap insertAnnouncement(AnnouncementPojo announcementPojo) {
        ModelMap modelMap = new ModelMap();
        boolean b = announcementService.save(announcementPojo);
        if (b == true) {
            modelMap.addAttribute("message", "添加成功");
        } else {
            modelMap.addAttribute("message", "添加失败");
        }
        return modelMap;
    }
    //删除按钮事件处理函数
    @ResponseBody
    @RequestMapping("/Announcement_remove")
    public ModelMap deleteAnnouncement(@RequestParam("ids")String[] ids){
        ModelMap modelMap = new ModelMap();
        if (ids!=null && ids.length>0){
            for (String id:ids
                 ) {
                boolean b = announcementService.removeById(id);
                if (b==true){
                    modelMap.addAttribute("message", "删除成功");
                }else{
                    modelMap.addAttribute("message", "删除失败");
                }

            }
        }
        return modelMap;
    }

    //修改按钮事件处理函数
    @RequestMapping("/toUpdateAnnouncement")
    public String toUpdateAnnouncement(String id,ModelMap modelMap){
        AnnouncementPojo byId = announcementService.getById(id);
        modelMap.addAttribute("announcementVO",byId);
        List<SystemUser> list = userService.list();
        modelMap.addAttribute("userList",list);
        List<DepartmentPojo> list1 = departmentService.list();
        modelMap.addAttribute("departmentPojo",list1);
        return "/views/manage/announcement/update_announcement";
    }
    //修改按钮事件处理函数
    @ResponseBody
    @RequestMapping("/AnnouncementUpdate")
    public ModelMap announcementUpdate(AnnouncementPojo announcementPojo){
        ModelMap modelMap = new ModelMap();
        boolean b = announcementService.updateById(announcementPojo);
        if (b==true){
            modelMap.addAttribute("message", "修改成功");
        }else {
            modelMap.addAttribute("message", "修改失败");
        }
        return modelMap;
    }
    //根据产品名称和产品价格相查询分业展示

    @ResponseBody
    @RequestMapping("/announcementFindLikePage")
    public ResultData announcementFindLikePage(long page,long rows,@RequestParam("announcementVO.title")String title,@RequestParam("announcementVO.department")String department,ModelMap modelMap){
        List<AnnouncementPojo> list = announcementService.announcementLikeList(page-1, rows, title, department);
        System.out.println(list);
        modelMap.addAttribute("result",list);
        modelMap.addAttribute("totalRows",list.size());
        ResultData resultData = ResultData.buildFailureResult("data", modelMap);
        return resultData;
    }


    @RequestMapping("/TaskAction_modify")
    public String taskAction_modify(@RequestParam("taskVo.id")String id,ModelMap modelMap){
        AnnouncementPojo byId = announcementService.getById(id);
        modelMap.addAttribute("announcementVO",byId);
        List<SystemUser> list = userService.list();
        modelMap.addAttribute("userList",list);
        List<DepartmentPojo> list1 = departmentService.list();
        modelMap.addAttribute("departmentPojo",list1);
        return "/views/manage/announcement/update_announcement";
    }
    @ResponseBody
    @RequestMapping("/TaskAction_editProduct")
    public ModelMap  announcementFindById(@RequestParam("taskVo.id")String id){
        ModelMap modelMap = new ModelMap();
                boolean b = announcementService.removeById(id);
                if (b==true){
                    modelMap.addAttribute("message", b);
                    modelMap.addAttribute("message", "删除成功");
                }else {
                    modelMap.addAttribute("message", b);
                    modelMap.addAttribute("message", "删除失败");
                }
        return modelMap;
    }
}
